namespace JQuery.WebKit
{
    using System;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;
    
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class BaseItem : IBaseItem, INamingContainer
    {
        private object _dataItem;
        private bool _isTrackingViewState;
        private StateBag _statebag = new StateBag();

        protected virtual void LoadViewState(object savedState)
        {
            object[] objArray = (object[]) savedState;
            if (objArray.Length != 1)
            {
                throw new ArgumentException("Invalid View State");
            }
            ((IStateManager) this.ViewState).LoadViewState(objArray[0]);
        }

        protected virtual object SaveViewState()
        {
            object[] objArray = new object[1];
            if (this.ViewState != null)
            {
                objArray[0] = ((IStateManager) this.ViewState).SaveViewState();
            }
            return objArray;
        }

        internal void SetDirty()
        {
            this.ViewState.SetDirty(true);
        }

        void IStateManager.LoadViewState(object state)
        {
            this.LoadViewState(state);
        }

        object IStateManager.SaveViewState()
        {
            return this.SaveViewState();
        }

        void IStateManager.TrackViewState()
        {
            this.TrackViewState();
        }

        protected virtual void TrackViewState()
        {
            this._isTrackingViewState = true;
            if (this.ViewState != null)
            {
                ((IStateManager) this.ViewState).TrackViewState();
            }
        }

        protected virtual bool IsTrackingViewState
        {
            get
            {
                return this._isTrackingViewState;
            }
        }

        bool IStateManager.IsTrackingViewState
        {
            get
            {
                return this.IsTrackingViewState;
            }
        }

        protected StateBag ViewState
        {
            get
            {
                return this._statebag;
            }
        }

        #region IBaseItem Members

        void IBaseItem.SetDirty()
        {
            this.SetDirty();
        }

        #endregion
    }
}

